package com.foreknow.dao.impl;

import com.foreknow.bean.Users;
import com.foreknow.dao.BaseDao;
import com.foreknow.dao.UsersDao;
import com.foreknow.mapping.EntityMapping;
import com.foreknow.mapping.MappingFactory;

import java.sql.SQLException;
import java.util.List;

public class UsersDaoImpl extends BaseDao implements UsersDao {
    @Override
    public int add(Users users) throws SQLException {
//        DBManager dbManager = DBManager.getInstance();
//        Connection conn = dbManager.getConnection();
//        JdbcTemplate jdbcTemplate = JdbcTemplate.getInstance();
//        jdbcTemplate.setConnection(conn);
        String sql = "insert into users(adminname,password) values (?,?)";
        int a = jdbcTemplate.update(sql,users.getAdminname(),users.getPassword());
        return a;
    }

    @Override
    public Users login(String adminname, String password) throws SQLException{
//        DBManager dbManager = DBManager.getInstance();
//        Connection conn = dbManager.getConnection();
//        JdbcTemplate jdbcTemplate = JdbcTemplate.getInstance();
//        jdbcTemplate.setConnection(conn);
//        MappingFactory mappingFactory = MappingFactory.getmf();
        String sql = "select * from users where adminname = ? and password = ?";
        EntityMapping entityMapping = mappingFactory.getmapping(MappingFactory.USERS_MAPPING);
        List<Object> list = jdbcTemplate.query(sql,entityMapping,adminname,password);
        if (list.size()>0){
            Users users = (Users) list.get(0);
            return users;
        }
        return null;
    }

//    public static void main(String[] args) {
//        UsersDaoImpl usersDao = new UsersDaoImpl();
//        String name = "admin";
//        String password = "admin";
//        try {
//            usersDao.login(name,password);
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        Users users = new Users();
//        users.setAdminname("tom");
//        users.setPassword("09786");
//        int a = 0;
//        try {
//            a = usersDao.add(users);
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        System.out.println(a);
//    }
}
